package cm.mefobe.android.telecomcost.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ProviderPickerView extends LinearLayout {

	private Context _context;
	private TextView _label;
	private ProviderSpinner _providerSpinner;

	public ProviderPickerView(Context context, String label) {
		super(context);
		Log.i("ProviderPickerView", "creating the ProviderPickerView for "
				+ label);
		_context = context;
		_label.setText(label);
		setProviderPickerView();
	}

	private void setProviderPickerView() {
		if (isInEditMode()) {
			Log.i("ProviderPickerView", "isInEditMode");
		} else {
			if (_providerSpinner == null) {
				Log.i("ProviderPickerView", "adding views...");
				setLayoutParams(new LayoutParams(
						android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
						android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
				setOrientation(LinearLayout.VERTICAL);
				_label = new TextView(_context);
				_providerSpinner = new ProviderSpinner(_context);
				
				_label.setText("Provider ");

				addView(_label);
				addView(_providerSpinner);
			} else {
				Log.i("ProviderPickerView", "already set");
			}
		}
	}

	public ProviderPickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("ProviderPickerView",
				"creating the time picker view with AttributeSet");
		_context = context;
		setProviderPickerView();
	}

	public ProviderPickerView(Context context) {
		super(context);
		Log.i("ProviderPickerView", "creating the ProviderPickerView");
		_context = context;
		setProviderPickerView();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("ProviderPickerView", "onLayout changed : " + changed);		
		getProviderSpinner().setAdapter(null);		
		super.onLayout(changed, l, t, r, b);		
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("ProviderPickerView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("ProviderPickerView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("ProviderPickerView", "onFinishInflate");
		super.onFinishInflate();
		setProviderPickerView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("ProviderPickerView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public void setLabel(String label) {
		if (_label != null) {
			_label.setText(label);
		} else {
			Log.i("ProviderPickerView",
					"cannot set the label...textView is null");
		}
	}

	public ProviderSpinner getProviderSpinner() {
		return _providerSpinner;
	}
}
